

//index.js
//获取应用实例
var app = getApp();
//获得

var MyRequest = require("../../utils/MyRequest.js");
var Global = require("../../Global.js");
var Cart = require("../../LocalCart.js");


Page({
  //页面初始化的数据
  data: {
    Switch: true,
    Page: 1,
    Count: 10,
    userinfo: '',
    cityId: 1,
    Citys: "",
    index: 0,
    cartDefaultStatus: "show",
    cartActiveStatus: "hide",
    ActiveDay: "",
    wDay: "",
    CartCount: Cart.countCartNum(),
    CartPrice: Cart.countCartPrice(),
    isShowBigImg: "hide",
    slideAds: 0,
    slideAdsIndex: 0,
    slideStr1: "",
    slideStr2: "",
  },


  //请求数据者
  Presenter: {
    getHome: function (that) {
      var data = { "action": "home_info", cityId: that.data.cityId };
      MyRequest.MyGetWithToast("Home", that, that.Binder.bindHome, data);
    },
    getActiveDay: function (that) {
      var data = { "action": "weekday" }
      MyRequest.MyGet("ActiveDay", that, that.Binder.bindDate, data);
    },
    Refresh: function (that) {
      var data = { "action": "home_info", cityId: that.data.cityId };
      MyRequest.MyGetWithRefresh("Home", that, that.Binder.bindHome, data)
    },
    Pull: function (that) {
      that.setData({
        Page: that.data.Page + 1
      })
      var data = {
        action: "findAllHotProduct",
        cityId: that.data.cityId,
        begin: 0,
        end: that.data.Page * that.data.Count
      }
      MyRequest.MyGetWithToast("HotProduct", that, that.Binder.bindHot, data);
    },
    getCitys: function (that) {
      var data = {
        action: "list"
      }
      MyRequest.MyGet("Citys", that, function (result) {
        that.setData({
          Citys: result.data.data
        })
        //绑定数据
      }, data)
    }

  },
  //绑定数据者
  Binder: {
    bindHome: function (result, that) {
      var data = result.data.data;
      that.setData({
        centerAd: data.centerAd,
        notice: data.notice,
        productClasses: data.productClasses,
        dailyActivity: data.dailyActivity,
        hotProduct: Cart.solveProductData(data.hotProduct, that.data.ActiveDay),
        newProduct: Cart.solveProductData(data.newProduct, that.data.ActiveDay)
      })
      that.setSlideAds();
    },
    bindDate: function (result, that) {
      var data = result.data.data;
      var activeDate = "1";
      var week = "";
      for (var i = 0; i < data.length; i++) {
        var item = data[i];
        if (item.state == "2" || item.state == 2) {
          activeDate = item.wdate;
          week = item.wday;
          break;
        }
      }
      that.setData({
        ActiveDay: activeDate,
        wDay: week
      });
      that.Presenter.getHome(that);
    },
    bindHot: function (result, that) {
      that.setData({
        hotProduct: Cart.solveProductData(result.data.data, that.data.ActiveDay)
      })
    }
  },
  onSwitch: function (event) {
    if (event.target.id == "switch-hot") {
      if (this.data.Switch) {
        return;
      }
      this.setData({
        Switch: true
      })

    } else {
      if (!this.data.Switch) {
        return;
      }
      this.setData({
        Switch: false
      })
    }
  },
  OnCityChange: function (event) {

    this.setData({
      index: event.detail.value,
    })
    this.setData({
      cityId: this.data.Citys[this.data.index].id
    });
    Global.setCity(this.data.Citys[this.data.index].id, this.data.Citys[this.data.index].cityName);
    this.Presenter.getHome(this);
    


  },
  addCart: function (event) {
    var product = event.target.dataset.product;
    Cart.setCartStorage(product, this.data.ActiveDay, this.data.wDay);
    this.setData({
      CartCount: Cart.countCartNum(),
      CartPrice: Cart.countCartPrice(),
      hotProduct: Cart.solveProductData(this.data.hotProduct, this.data.ActiveDay),
      newProduct: Cart.solveProductData(this.data.newProduct, this.data.ActiveDay)
    });
  },
  subCart: function (event) {
    var product = event.target.dataset.product;
    Cart.subCartNum(product, this.data.ActiveDay);
    this.setData({
      CartCount: Cart.countCartNum(),
      CartPrice: Cart.countCartPrice(),
      hotProduct: Cart.solveProductData(this.data.hotProduct, this.data.ActiveDay),
      newProduct: Cart.solveProductData(this.data.newProduct, this.data.ActiveDay)
    });
  },
  toCartPage: function (event) {
    if (!Global.getUserStorage()) {
      wx.navigateTo({
        url: '/pages/login/login'
      })
    } else {
      wx.navigateTo({
        url: '/pages/cart/cart'
      });
    }
  },
  //页面初次渲染
  onReady: function () {
    var cityId = 1;
    if(Global.getCityId()){
      cityId=Global.getCityId()
    }else if (Global.getUserStorage().cityId){
      cityId=Global.getUserStorage().cityId;
    }
    this.setData({
      userinfo: Global.getUserStorage(),
      cityId: cityId,
      index:cityId - 1
    })
    this.Presenter.getCitys(this);
  },

  onLoad: function () {
    this.Presenter.getActiveDay(this);
  },
  //页面显示
  onShow: function () {
    this.setData({
      CartCount: Cart.countCartNum(),
      CartPrice: Cart.countCartPrice(),
      hotProduct: Cart.solveProductData(this.data.hotProduct, this.data.ActiveDay),
      newProduct: Cart.solveProductData(this.data.newProduct, this.data.ActiveDay)
    });
  },
  //页面隐藏
  onHide: function () { },
  //页面删除
  onUnload: function () { },
  //用户下拉
  onPullDownRefresh: function () {
    // 阻止下拉
    this.setData({
      Page: 1
    })
    this.Presenter.Refresh(this);
    this.Presenter.getActiveDay(this);

  },
  //下拉触底
  onReachBottom: function () {
    this.Presenter.Pull(this);
  },
  //点击类型
  toProductList: function (event) {
    var data = "?";
    data += "classId=";
    data += event.currentTarget.dataset.classid;
    data += "&detailImage=";
    data += event.currentTarget.dataset.detailimage;
    data += "&closetime=";
    data += event.currentTarget.dataset.closetime;
    data += "&className=";
    data += event.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/productList/productList' + data
    })
  },
  showBigImage: function (event) {
    var url = "http://static.52letsgo.cn/" + event.currentTarget.dataset.imgurl;
    this.setData({
      isShowBigImg: "show",
      bigImage: url
    });
  },
  hideBigImg: function () {
    this.setData({
      isShowBigImg: "hide"
    });
  },
   onShareAppMessage: function () {
    return {
      title: '早范早餐',
      path: '/page/index'
    }
  },
  bannerClick:function(event){
  
    var data = event.currentTarget.dataset.title;
     if(data!="充值优惠"){
       return ;
     }
     if(!Global.getUserStorage()){
      wx.navigateTo({url: '/pages/login/login',})
       return;
     }
    var params = "?balance=";
      params += this.data.userinfo.money;
     wx.navigateTo({url: '/pages/balance/balance'+params})
  },
  setSlideAds: function () {
    if(this.slidetimer != null || this.slidetimer != undefined){
      clearInterval(this.slidetimer);
    }
    var that = this;
    var notices = this.data.notice;
    var index = 0;
    var nSize = notices.length;
    if (nSize > 0) {
      var str = notices[0].mobile
        + "点了 " + notices[0].productName
        + " " + notices[0].productCount
        + "份";
      this.setData({
        slideStr1: str
      });
    }
    if (nSize > 2) {
      var str = notices[1].mobile
        + "点了 " + notices[1].productName
        + " " + notices[1].productCount
        + "份";
      this.setData({
        slideStr2: str,
        slideAdsIndex:1
      });
      index = 1;
      this.slidetimer = setInterval(function () {
        var animationShow = wx.createAnimation({
          duration: 1000,
          timingFunction: 'ease',
        });
        animationShow.left(0).step();
        var animationHide = wx.createAnimation({
          duration: 1000,
          timingFunction: 'ease',
        });
        animationHide.left("-100%").step();
        animationHide.left("100%").step({ duration: 0, });
        index ++;
        if(index >= nSize){
          index = 0;
        }
        var str = notices[index].mobile
        + "点了 " + notices[index].productName
        + " " + notices[index].productCount
        + "份";
        if (that.data.slideAds == 0) {
          that.setData({
            animationData: animationShow.export(),
            animationData1: animationHide.export(),
            slideAds: 1,
            slideStr1: str,
          });
        } else {
          that.setData({
            animationData1: animationShow.export(),
            animationData: animationHide.export(),
            slideAds: 0,
            slideStr2: str,
          });
        }
      }, 3000);
    }


  }
})

